<template>
    <div style="width: 60%;padding-left: 20%;padding-top: 6%">
        <el-form :model="listInfo" :rules="rules" ref="listInfo" class="demo-ruleForm">
            <el-form-item label="标题" prop="title">
                <el-input v-model="listInfo.title" placeholder="请输入你的文章的主标题"></el-input>
            </el-form-item>
            <el-form-item label="副标题" prop="subtitle">
                <el-input v-model="listInfo.subtitle" placeholder="请输入文章副标题，可省略"></el-input>
            </el-form-item>
            <el-form-item label="标签" prop="label">
                <el-checkbox-group v-model="listInfo.label">
                    <el-checkbox label="风景"></el-checkbox>
                    <el-checkbox label="建筑"></el-checkbox>
                    <el-checkbox label="美女"></el-checkbox>
                    <el-checkbox label="植物"></el-checkbox>
                    <el-checkbox label="动物"></el-checkbox>
                    <el-checkbox label="动漫"></el-checkbox>
                    <el-checkbox label="游戏"></el-checkbox>
                    <el-checkbox label="科技"></el-checkbox>
                    <el-checkbox label="产品"></el-checkbox>
                    <el-checkbox label="其它"></el-checkbox>
                </el-checkbox-group>
            </el-form-item>
            <el-form-item label="上传图片" prop="img" >
                <el-upload
                        class="upload-demo"
                        action="https://localhost:8848/post/img"
                        list-type="picture"
                        :on-change="deal">
                    <el-button size="small" type="primary">点击上传</el-button>
                    <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
                </el-upload>
                <el-button @click="uploadImg" plain>使用图片编辑服务</el-button>
                <div class="el-upload__tip">⚠️请注意，跳转后此页面内容不会被保存</div>
            </el-form-item>
            <el-form-item label="内容" prop="content">
                <el-input type="textarea" v-model="listInfo.content" placeholder="请输入文章内容，不少于6个字符"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm">立即创建</el-button>
                <el-button @click="resetForm">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    export default {
        name: "addList",
        data:function () {
            return{
                listInfo:{
                    title:'',
                    subtitle:'',
                    author:'',
                    content:'',
                    img:[],
                    date:'',
                    read:0,
                    like:0,
                    label:[],
                    abstract:'',
                },
                rules: {
                    content: [
                        { required: true, message: '请输入文章内容', trigger: 'blur' },
                        { min: 6, message: '长度必须大于6个字符', trigger: 'blur' }
                    ],
                    title: [
                        { required: true, message: '请输入文章标题', trigger: 'blur' },
                        { min: 1,max:20, message: '长度在1～20个字符之间', trigger: 'blur' }
                    ],
                    label: [
                        { required: true, message: '请选择标签', trigger: 'change' }
                    ],
                    img:[
                        {required:true,message:'请上传图片',trigger:'click'}
                    ]
                }
            }
        },
        methods:{
            uploadImg:function () {
                this.$router.push('./simps');
            },
            submitForm:function () {
                let xhr = new XMLHttpRequest();
                xhr.withCredentials = true;
                let that = this;
                xhr.open('POST','https://localhost:8848/uploadlist');
                xhr.onreadystatechange = function () {
                    if (xhr.readyState===4&&xhr.status===200){
                        let text = xhr.responseText;
                        if (text==='true'){
                            that.$notify.success({
                                title:'发布成功',
                                message:'即将为你跳转到首页'
                            });
                            that.$router.push('/home');
                        }else{
                            that.$notify.error({
                                title:'错误',
                                message:'无法发布成功，请重试'
                            })
                        }
                    }
                };
                xhr.setRequestHeader('Content-Type',"application/json; charset=utf-8");
                xhr.send(JSON.stringify(this.listInfo));
            },
            resetForm:function () {
                this.listInfo = {};
            },
            deal:function (file,fileList) {
                for(let i = 0; i < fileList.length; i++){
                    if (this.listInfo.img.indexOf(fileList[i].name)===-1) {
                        this.listInfo.img.push(fileList[i].name);
                    }
                }
            },
        }
    }
</script>

<style scoped src="../css/listStyle.css">

</style>